Completed
Push — master ( 309191...e78567 )
by Dongxin
03:33
created

common.js ➔ ... ➔ parse   F

Complexity

Conditions 20
Paths 255

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 20
c 3
b 0
f 1
nc 255
nop 2
dl 0
loc 84
rs 3.7005

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like common.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// Copyright © 2017 Tangdongxin
2
3
// Permission is hereby granted, free of charge, to any person obtaining
4
// A copy of this software and associated documentation files (the "Software"),
5
// To deal in the Software without restriction, including without limitation
6
// The rights to use, copy, modify, merge, publish, distribute, sublicense,
7
// And/or sell copies of the Software, and to permit persons to whom the
8
// Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included
10
// In all copies or substantial portions of the Software.
11
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
14
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
17
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
18
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20
21
let bgColor; // Background color
0 ignored issues
show
Unused Code introduced by
The variable bgColor seems to be never used. Consider removing it.
Loading history...
22
let intColor; // Integer color
0 ignored issues
show
Unused Code introduced by
The variable intColor seems to be never used. Consider removing it.
Loading history...
23
let strColor; // String color
0 ignored issues
show
Unused Code introduced by
The variable strColor seems to be never used. Consider removing it.
Loading history...
24
let keyColor; // K-v key color
0 ignored issues
show
Unused Code introduced by
The variable keyColor seems to be never used. Consider removing it.
Loading history...
25
let defaultColor; // Default text color
0 ignored issues
show
Unused Code introduced by
The variable defaultColor seems to be never used. Consider removing it.
Loading history...
26
let fontStyle; // Font-family
0 ignored issues
show
Unused Code introduced by
The variable fontStyle seems to be never used. Consider removing it.
Loading history...
27
let fontSize; // Font-size
0 ignored issues
show
Unused Code introduced by
The variable fontSize seems to be never used. Consider removing it.
Loading history...
28
let strictOnly; // Only deal with the application/json response
0 ignored issues
show
Unused Code introduced by
The variable strictOnly seems to be never used. Consider removing it.
Loading history...
29
let hideDetails; // Hide the count and size
30
let dontBeatify; // Hide the [str] or [json]
0 ignored issues
show
Unused Code introduced by
The variable dontBeatify seems to be never used. Consider removing it.
Loading history...
31
let strLength; // String length
0 ignored issues
show
Unused Code introduced by
The variable strLength seems to be never used. Consider removing it.
Loading history...
32
let isDebug; // Debug mode
33
// ===========================================
34
// DEFAULT VALUES
35
// ===========================================
36
const RAND = 'MIKE';
37
const HOV = `H${ RAND}`;
0 ignored issues
show
Unused Code introduced by
The constant HOV seems to be never used. Consider removing it.
Loading history...
38
const DIV = `D${ RAND}`;
39
const KEY = `K${ RAND}`;
40
const STR = `S${ RAND}`;
41
const BOOL = `B${ RAND}`;
42
const ERR = `E${ RAND}`;
43
const COLL = `C${ RAND}`;
0 ignored issues
show
Unused Code introduced by
The constant COLL seems to be never used. Consider removing it.
Loading history...
44
45
// ===========================================
46
// COMMON FUNCTIONS
47
// ===========================================
48
function onError (result, error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
50
  console.log(result);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
51
52
}
53
54
function dlog (target) {
55
56
  if (isDebug) {
0 ignored issues
show
Bug introduced by
The variable isDebug seems to be never initialized.
Loading history...
57
58
    console.log(target);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
59
60
  }
61
62
}
63
64
function reconvert (str) {
65
66
  str = str.replace(/(\\u)(\w{1,4})/gi, function ($0) {
67
68
    return String.fromCharCode(parseInt(escape($0).replace(/(%5Cu)(\w{1,4})/g, '$2'), 16));
69
70
  });
71
  str = str.replace(/(&#x)(\w{1,4});/gi, function ($0) {
72
73
    return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g, '$2'), 16));
74
75
  });
76
  str = str.replace(/(&#)(\d{1,6});/gi, function ($0) {
77
78
    return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g, '$2')));
79
80
  });
81
82
  return str;
83
84
}
85
86
function units (size) {
87
88
  return size > 1048576 ? `${0 | size / 1048576 }MB` :
89
    size > 1024 ? `${0 | size / 1024 }KB` :
90
      `${size }B`;
91
92
}
93
94
function fragment (div, a, b) {
95
96
  const frag = document.createDocumentFragment();
97
  frag.appendChild(document.createTextNode(a));
98
  if (b) {
99
100
    frag.appendChild(div.cloneNode());
101
    frag.appendChild(document.createTextNode(b));
102
103
  } else {
104
105
    frag.appendChild(document.createElement('br'));
106
107
  }
108
  return frag;
109
110
}
111
112
function change (node, query, name, set) {
113
114
  let list = node.querySelectorAll(query),
115
    i = list.length;
116
  for (; i--;) {
117
118
    list[i].classList[set ? 'add' : 'remove'](name);
119
120
  }
121
122
}
123
124
function draw (str, current, isEmbed = false) {
125
126
  const re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi;
127
  let node = document.createElement('div');
128
  node.classList.add(DIV);
129
  const link = document.createElement('a');
130
  const span = document.createElement('span');
131
  const info = document.createElement('i');
132
  const colon = document.createTextNode(': ');
133
  const comma = fragment(node, ',');
134
  const path = [];
135
  const cache = {
136
    '{': fragment(node, '{', '}'),
137
    '[': fragment(node, '[', ']'),
138
  };
139
140
  node.className = `R${ RAND}`;
141
  link.classList.add(`L${ RAND}`);
142
  if (isEmbed) {
143
144
    info.classList.add(`IJSON${ RAND}`);
145
146
  } else {
147
148
    info.classList.add(`I${ RAND}`);
149
150
  }
151
152
  parse(str, re);
153
154
  current.innerHTML = node.innerHTML;
155
156
  function parse (str, re) {
157
158
    str = reconvert(str);
159
    let match, val, tmp, i = 0;
0 ignored issues
show
Unused Code introduced by
The variable i seems to be never used. Consider removing it.
Loading history...
160
    const len = str.length;
161
    try {
162
163
      for (; match = re.exec(str);) {
164
165
        val = match[0];
166
        if (val == '{' || val == '[') {
167
168
          path.push(node);
0 ignored issues
show
Bug introduced by
The variable node is changed as part of the for loop for example by node.lastChild.previousSibling on line 170. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
169
          node.appendChild(cache[val].cloneNode(true));
170
          node = node.lastChild.previousSibling;
171
          node.len = 1;
172
          node.start = re.lastIndex;
173
174
        } else if ((val == '}' || val == ']') && node.len) {
175
176
          if (node.childNodes.length) {
177
178
            tmp = info.cloneNode();
179
            const content = node.len + (
180
              node.len == 1 ?
181
                val == ']' ? ' item, ' : ' property, ' :
182
                val == ']' ? ' items, ' : ' properties, '
183
            ) + units(re.lastIndex - node.start + 1);
184
185
            if (hideDetails) {
186
187
              tmp.setAttribute('title', content);
188
189
            } else {
190
191
              tmp.dataset.content = content;
192
193
            }
194
195
            if ((val = node.previousElementSibling) && val.className == KEY) {
196
197
              tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, '\\\''));
198
199
            }
200
            node.parentNode.insertBefore(tmp, node);
201
202
          } else {
203
204
            node.parentNode.removeChild(node);
205
206
          }
207
          node = path.pop();
208
209
        } else if (val == ',') {
210
211
          node.len += 1;
212
          node.appendChild(comma.cloneNode(true));
213
214
        } else {
215
216
          tmp = span.cloneNode();
217
          tmp.textContent = match[1] || val;
218
          tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL);
219
          node.appendChild(tmp);
220
          if (match[3]) {
221
222
            node.appendChild(colon.cloneNode());
223
224
          }
225
226
        }
227
228
      }
229
      document.title = '';
230
      JSON.parse(str);
231
232
    } catch (e) {
233
234
      dlog(e);
235
      // TODO: find a better way to report error
236
237
    }
238
239
  }
240
241
}
242